//
//  KYZXBaseVC.m
//  KuYouZiXun
//
//  Created by lanou on 15/8/4.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "KYZXBaseVC.h"

#import "KYZXScreen.h"

#import "KYZXUserInfo.h"

@interface KYZXBaseVC ()

@end

@implementation KYZXBaseVC

- (void)dealloc{

    [[KYZXUserInfo standardKYZXUserInfo] removeObserver:self];
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    //观察者 监听 单例的属性变化 更改配色方案夜间模式 或者 日间模式
    [[KYZXUserInfo standardKYZXUserInfo] addObserver:self];
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49)];
    self.automaticallyAdjustsScrollViewInsets = NO;//这个属性会导致vc向下便宜20 关掉
    [self.view addSubview:self.contentView];
    self.navigationController.navigationBarHidden = YES;//隐藏自带的navigationBar
    [self createTopBar];//创建顶栏视图
    //给glassView添加轻拍手势
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(regainLeftMeun)];
    [self.glassView addGestureRecognizer:tapGes];
    self.view.backgroundColor = [KYZXUserInfo standardKYZXUserInfo].backgroundColor;
    //添加屏幕边缘轻扫手势
    self.edgePan =[[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(doEdgePan:)];
    //添加支持的方向
    self.edgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:self.edgePan];
    
}


- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}


#pragma mark 控制左侧菜单的收回与推出

//收回左侧菜单
- (void)regainLeftMeun{
    [UIView animateWithDuration:0.5 animations:^{
        _leftMeunVC.view.frame = CGRectMake(-SCREEN_WIDTH, 0, SCREEN_WIDTH - 70, SCREEN_HEIGHT);
        self.glassView.alpha = 0;
    }];
}

//推出左侧菜单
- (void)pushLeftMeun{

    [UIView animateWithDuration:0.5 animations:^{
        self.leftMeunVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH - 70, SCREEN_HEIGHT);
        self.glassView.alpha = 0.5;
    }];
}


#pragma mark 屏幕边缘轻扫手势

- (void)doEdgePan:(UIScreenEdgePanGestureRecognizer *)edgePan{
    //捕捉变化位置
    CGPoint point = [edgePan translationInView:edgePan.view];
    CGRect newFame = self.leftMeunVC.view.frame;
    //视图跟随手指移动
    newFame.origin.x= -SCREEN_WIDTH + point.x;
    self.leftMeunVC.view.frame = newFame;
    //滑动时候 他的alpha值动态改变
    self.glassView.alpha = point.x / (SCREEN_WIDTH) * 0.5;
    //当手势结束时
    if (edgePan.state == UIGestureRecognizerStateEnded) {
        if (point.x > 150) {
            [self pushLeftMeun];
        }
        else{
            [self regainLeftMeun];
        }
    }
}


#pragma mark 创建顶栏视图

- (void)createTopBar{
    
    self.topBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
    self.topBarView.backgroundColor = [KYZXUserInfo standardKYZXUserInfo].backgroundColor;;
    self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftButton.frame = CGRectMake(0, 0, 44, 44);
    [self.leftButton setImage:[UIImage imageNamed:@"news_settings"] forState:UIControlStateNormal];
    [self.leftButton addTarget:self action:@selector(doTapLeftButton:) forControlEvents:UIControlEventTouchUpInside];
    [_topBarView addSubview:_leftButton];
    [self.view addSubview:_topBarView];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, 1)];
    lineView.backgroundColor = [UIColor grayColor];
    [self.topBarView addSubview:lineView];
    
}


//顶栏左按钮 默认pop上一个视图
- (void)doTapLeftButton:(UIButton *)button{
    
    [self pushLeftMeun];
}


//重写setLeftMeunVC 让他的view frame
- (void)setLeftMeunVC:(KYZXLeftMeunVC *)leftMeunVC{

    _leftMeunVC = leftMeunVC;
    _leftMeunVC.view.frame = CGRectMake(-SCREEN_WIDTH, 0, SCREEN_WIDTH - 70, SCREEN_HEIGHT);
}


//他的子类只要重写这个方法就可以改变别的配色
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

    self.topBarView.backgroundColor = [KYZXUserInfo standardKYZXUserInfo].backgroundColor;
     self.view.backgroundColor = [KYZXUserInfo standardKYZXUserInfo].backgroundColor;
}

@end
